<template>
  <div class="echart-box">
    <div class="echart-title">租/用户情况</div>
    <div class="echart-cont">
      <div class="pie-tenant" v-if="isManagerShow">
        <div class="pieCont" ref="pieTenant"></div>
        <div class="pieInfo">
          <div class="downBlock">
            <div class="blueBlock" v-if="colorShow"></div>
            <div>
              <span class="textBlock">{{this.noLock}}</span>
              <span>{{this.tenantNum}}</span>
            </div>
          </div>
          <div class="downBlock">
            <div class="grayBlock" v-if="colorShow"></div>
            <div>
              <span class="textBlock">{{this.lock}}</span>
              <span>{{this.tenantLockNum}}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="pie-user">
        <div class="pieCont" ref="pieUser"></div>
        <div class="pieInfo">
          <div class="downBlock">
            <div class="blueBlock" v-if="colorShow"></div>
            <div>
              <span class="textBlock">{{this.noLock}}</span>
              <span>{{this.userNum}}</span>
            </div>
          </div>
          <div class="downBlock">
            <div class="grayBlock" v-if="colorShow"></div>
            <div>
              <span class="textBlock">{{this.lock}}</span>
              <span>{{this.userLockNum}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import roleMixin from "@/mixins/role";
import echarts from "echarts";
export default {
  name: "OpenSituation",
  mixins: [roleMixin],
  props: {
    situation: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      isManagerShow: true, //判断登录身份
      tenantLockNum: "", //租户锁定数量
      tenantNum: "", //租户未锁定数量
      userLockNum: "", //用户锁定数量
      userNum: "", //用户为锁定数量
      noLock: "", //未锁定
      lock: "", //已锁定
      colorShow: false, //加载方法完毕后显示字段
    };
  },
  mounted() {
    if (this.isManager) {
      this.isManagerShow = true;
    } else {
      this.isManagerShow = false;
    }
  },
  watch: {
    // 监听sitution值是否传到
    situation: {
      handler() {
        this.homeInfo();
      },
      deep: true
    }
  },
  methods: {
    // 判断登录身份后调方法
    homeInfo() {
      const situation = this.situation;
      this.lock = "已锁定";
      this.noLock = "未锁定";
      this.colorShow = true;
      this.tenantLockNum = situation.tenantLockNum
        ? situation.tenantLockNum
        : 0;
      this.tenantNum = situation.tenantNum ? situation.tenantNum : 0;
      this.userLockNum = situation.userLockNum ? situation.userLockNum : 0;
      this.userNum = situation.userNum ? situation.userNum : 0;
      if (this.isManager) {
        this.initPieTenant();
        this.initPieUser();
      } else {
        this.initPieUser();
      }
    },
    // 租户数量
    initPieTenant() {
      let pieTenant = echarts.init(this.$refs.pieTenant);
      pieTenant.setOption({
        title: {
          text: "租户数量",
          x: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        series: [
          {
            name: "租户数量",
            type: "pie",
            radius: "45%",
            center: ["50%", "45%"],
            data: [
              { value: this.tenantNum, name: "未锁定" },
              { value: this.tenantLockNum, name: "已锁定" }
            ],
            color: ["#409EFF", "#909399"],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      });
    },
    // 用户数量
    initPieUser() {
      let pieUser = echarts.init(this.$refs.pieUser);
      pieUser.setOption({
        title: {
          text: "用户数量",
          x: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        series: [
          {
            name: "用户数量",
            type: "pie",
            radius: "45%",
            center: ["50%", "45%"],
            data: [
              { value: this.userNum, name: "未锁定" },
              { value: this.userLockNum, name: "已锁定" }
            ],
            color: ["#409EFF", "#909399"],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.echart-box {
  min-width: 49%;
  max-width: 50%;
  height: 400px;
  background: #fff;
  border-radius: 4px;
  overflow: hidden;
}
.echart-title {
  width: calc(100% - 20px);
  height: 30px;
  font-size: 14px;
  line-height: 32px;
  padding-left: 20px;
  margin: 10px 10px 0 10px;
  border-radius: 4px 4px 0 0;
  background: #e4e7ed;
}
.echart-cont {
  width: calc(100% - 20px);
  height: calc(100% - 50px);
  margin: 0 10px 10px 10px;
  border: 1px solid #dcdfe6;
  border-radius: 0 0 4px 4px;
  display: flex;
  flex-flow: row nowrap;
  justify-content: center;
  align-items: center;
  .pie-tenant,
  .pie-user {
    width: 50%;
    height: 100%;
    padding: 20px 0;
    display: flex;
    flex-flow: column nowrap;
    justify-content: flex-start;
    align-items: center;
    .pieCont {
      width: 100%;
      height: 80%;
    }
    .pieInfo {
      width: 100%;
      display: flex;
      flex-flow: column nowrap;
      justify-content: center;
      align-items: flex-start;
      .downBlock {
        width: 100%;
        height: 100%;
        display: flex;
        flex-flow: row nowrap;
        justify-content: flex-start;
        align-items: center;
        margin-bottom: 5px;
        padding-left: 10px;
        .blueBlock {
          background: #409eff;
          border-radius: 4px;
          width: 30px;
          height: 15px;
        }
        .grayBlock {
          background: #909399;
          border-radius: 4px;
          width: 30px;
          height: 15px;
        }
        .textBlock{
          margin: 0 5px;
        }
      }
    }
  }
}
</style>